package fei.yuzhi.com.loveflowers.ui;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.update.BmobUpdateAgent;
import cn.jpush.android.api.CustomPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface;
import fei.yuzhi.com.loveflowers.R;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private RadioGroup rg_table;
    private ViewPager vp_zhu;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EventBus.getDefault().register(this);
        setContentView(R.layout.activity_main);
        rg_table = (RadioGroup) findViewById(R.id.rg_table);
        vp_zhu = (ViewPager) findViewById(R.id.vp_zhu);
        Bmob.initialize(this, "7c6d1a4bf3aafd9f814667d6ad3cf6bb");
        //发起自动更新
        BmobUpdateAgent.update(this);
        //极光
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
        //极光自定义
        CustomPushNotificationBuilder builder = new
                CustomPushNotificationBuilder(MainActivity.this,
                R.layout.item_tuisong,
                R.id.item_tuisong_iv,
                R.id.item_tuisong_title_tv,
                R.id.item_tuisong_text_tv);
        // 指定定制的 Notification Layout
        builder.statusBarDrawable = R.mipmap.ic_loverfowers;
        // 指定最顶层状态栏小图标
        builder.layoutIconDrawable = R.mipmap.ic_loverfowers;
        // 指定下拉状态栏时显示的通知图标
        JPushInterface.setPushNotificationBuilder(2, builder);
        //友盟统计
        MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL);



        for (int i=0;i<rg_table.getChildCount();i++){
            rg_table.getChildAt(i).setTag(i);
            rg_table.getChildAt(i).setOnClickListener(this);
        }

        MainAdapter zhuAdpater=new MainAdapter(getSupportFragmentManager(),rg_table.getChildCount());
        vp_zhu.setAdapter(zhuAdpater);
        vp_zhu.setOffscreenPageLimit(4);
        //viewpager联动导航条
        vp_zhu.addOnPageChangeListener( new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                ((RadioButton)rg_table.getChildAt(position)).setChecked(true);
            }
        });



    }
    @Subscribe(threadMode= ThreadMode.MAIN)
    public void getJumppager(int pager ){
        vp_zhu.setCurrentItem(pager);
    }



    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rg_table_zhuye:
            case R.id.rg_table_baozhao:
            case R.id.rg_table_found:
            case R.id.rg_table_me:
                vp_zhu.setCurrentItem((int) view.getTag());
            break;
        }

    }


    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
